* Table1.do  09/21/22
* Data for summary statistics table

use "PopSeriesAnnual.dta" if year==2020, clear
replace year = 2021
tempfile fi21
save `fi21', replace
use "PopSeriesAnnual.dta" if year<=2020, clear
append using `fi21'
sort gender age race state place year
save `fi21', replace

use "DeathSeriesAnnual.dta" if (race=="All" | race==`"{"White"}"' | race==`"{"Black or African American"}"') & gender=="All" & age=="0+" & ucd=="Drug" & (mcd=="T402T403" | mcd=="T400T401T404" | mcd=="Opioid"), clear
sort gender age race state place year
merge gender age race state place year using `fi21'
drop if gender!="All" | age!="0+"
tab _merge
drop _merge

gen byte lateyears = year>=2013
collapse (sum) pop deaths, by(race lateyears state mcdtitle)
save `fi21', replace

keep if race=="All"
encode mcdtitle, gen(mcdnum)
reshape groups mcdnum 1 2 3
reshape cons state lateyears pop
reshape var deaths
reshape wide
ren deaths1 deathsall  // will be less than Rx + Im due to poly drug use
ren deaths2 deathsim
ren deaths3 deathsrx
gen polysh = (deathsim + deathsrx)/deathsall - 1
gen deathrtall = deathsall*100000/pop
gen imsh = deathsim/(deathsim+deathsrx)
gen rxsh = 1 - imsh
gen deathrtrx = (1-imsh)*deathrtall
gen deathrtim = imsh*deathrtall

* summary stats for all races
sort lateyears state
list state lateyears deathrtall rxsh, sep(0)
tabstat pop deaths*, stat(sum) format(%14.0fc) by(lateyears)

* summary stats for black population share
use pop lateyears race state if race!="All" using `fi21', clear
gen byte black = race==`"{"Black or African American"}"'
collapse (sum) pop, by(black state)
reshape groups black 0 1
reshape cons state
reshape var pop
reshape wide
ren pop0 popwhite
ren pop1 popblack
gen blacksh = popblack/(popblack+popwhite)
list state blacksh, sep(0)
tabstat pop*, stat(sum) format(%14.0fc)
